<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>页面滚动事件</title>
  <style>
    body {
      height: 3000px;
    }

    div {
      margin-top: 50px;
      width: 200px;
      height: 200px;
      background-color: rebeccapurple;
      /* display: block; */
      display: none;
    }
  </style>
</head>

<body>
  <div></div>
  <script>
    const div = document.querySelector('div')
    document.addEventListener('scroll', function (e) {
      // 获取HTML
      console.log(document.documentElement.scrollTop);
      const n = document.documentElement.scrollTop
      if (n >= 20) {
        div.style.display = 'block'
      }
      // else {
      //   div.style.display = 'none'
      // }
    })
  </script>
</body>

</html>